package com.myweather.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.myweather.android.R;
import com.myweather.android.activity.NewsWebActivity;
import com.myweather.android.bean.Toutiao;

import java.util.List;


/**
 * @author zcq
 * @package example.com.zcq.mymj.Adapter
 * @fileName MyMj
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/11/23  16:45
 */
public class TouTiaoAdapter extends RecyclerView.Adapter<TouTiaoAdapter.viewHolder> {
    private List<Toutiao.ResultBean.DataBean> mDataBeanList;
    private Context context;

    public TouTiaoAdapter(Context context, List<Toutiao.ResultBean.DataBean> contentlistBeanList) {
        mDataBeanList = contentlistBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item, viewGroup,false);
        viewHolder viewHolder = new viewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final viewHolder viewHolder, int i) {
        final Toutiao.ResultBean.DataBean[] data = {mDataBeanList.get(i)};
        viewHolder.mGgTitle.setText(data[0].getTitle());
        RequestOptions options = new RequestOptions()
//                .override(200, 100)
                .placeholder(R.drawable.new_pic)				//加载成功之前占位图
                .error(R.drawable.new_pic)
                .fitCenter();
//
        Glide.with(context)
                .asBitmap()
                .load(data[0].getThumbnail_pic_s())
                .apply(options)
                .into(viewHolder.mIvGg1);
        Glide.with(context)
                .asBitmap()
                .load(data[0].getThumbnail_pic_s02())
                                .apply(options)
                .into(viewHolder.mIvGg2);
        Glide.with(context)
                .asBitmap()
                .load(data[0].getThumbnail_pic_s03())
                                .apply(options)
                .into(viewHolder.mIvGg3);
        viewHolder.mGgTime.setText(data[0].getDate());
        viewHolder.mGgCount.setText(data[0].getAuthor_name());

        viewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int postion = viewHolder.getAdapterPosition();
                Toutiao.ResultBean.DataBean d = mDataBeanList.get(postion);
                context.startActivity(new Intent(context, NewsWebActivity.class).putExtra("url",d.getUrl()));

            }
        });


    }

    @Override
    public int getItemCount() {
        return mDataBeanList.size();
    }

    static class viewHolder extends RecyclerView.ViewHolder {
        View view;
        TextView mGgTitle;
        ImageView mIvGg1;
        ImageView mIvGg2;
        ImageView mIvGg3;
        TextView mGgCount;
        TextView mGgTime;

        public viewHolder(@NonNull View itemView) {
            super(itemView);
            this.view = itemView;
            this.mGgTitle = (TextView) view.findViewById(R.id.gg_title);
            this.mIvGg1 = (ImageView) view.findViewById(R.id.iv_pic1);
            this.mIvGg2 = (ImageView) view.findViewById(R.id.iv_pic2);
            this.mIvGg3 = (ImageView) view.findViewById(R.id.iv_pic3);
            this.mGgCount = (TextView) view.findViewById(R.id.gg_count);
            this.mGgTime = (TextView) view.findViewById(R.id.gg_time);
        }
    }

}
